﻿Imports System.IO
Public Class blog
    Shared iPosInItem As Integer

    Dim mBlogItems As New Generic.List(Of BlogItem)
    Public Property BlogItems() As Generic.List(Of BlogItem)
        Get
            Return mBlogItems
        End Get
        Set(ByVal value As Generic.List(Of BlogItem))
            mBlogItems = value
        End Set
    End Property
    Private Shared Function GetTagVal(ByVal sBText As String, ByRef sTag1 As String, ByRef stag2 As String) As String
        If sTag1 <> vbLf Then sTag1 = sTag1 & ":"
        Select Case stag2
            Case vbLf, "-----"
            Case Else
                stag2 = stag2 & ":"

        End Select
       
        Dim iLen As Integer = sTag1.Length
        Dim sres As String = sBText.Substring(sBText.IndexOf(sTag1, iPosInItem) + iLen, sBText.IndexOf(stag2, sBText.IndexOf(sTag1, iPosInItem) + iLen) - (sBText.IndexOf(sTag1, iPosInItem) + iLen)).Trim
        iPosInItem = sBText.IndexOf(sTag1, iPosInItem) + iLen
        Return sres
    End Function
    Private Shared Function GetCommentDate(ByVal sBText As String) As String
        
        Dim stag1 As String = "DATE:"
        Dim stag2 As String = vbLf
        Dim iLen As Integer = sTag1.Length
        Dim sres As String = sBText.Substring(sBText.IndexOf(sTag1, iPosInItem) + iLen, sBText.IndexOf(stag2, sBText.IndexOf(sTag1, iPosInItem) + iLen) - (sBText.IndexOf(sTag1, iPosInItem) + iLen)).Trim
        iPosInItem = sBText.IndexOf(stag1, iPosInItem) + iLen + sres.Length + 2
        Return sres
    End Function
   
    Public Sub New(ByRef sFilename As String)
        Dim sr As New StreamReader(sFilename)
        Dim sBlog As String = sr.ReadToEnd
        Dim sBlogentries() As String
        Dim iPos As Integer = sBlog.IndexOf("--------" & vbLf)
        Dim iPos2 As Integer = 0
        Dim i As Integer = 0
        Do While iPos > -1
            Dim BI As New BlogItem
            Dim sBText As String

            sBText = sBlog.Substring(iPos2, iPos)
            sBlog = sBlog.Replace(sBText, "")
            sBlog = sBlog.Substring(8, sBlog.Length - 8).Trim
            iPos = sBlog.IndexOf("--------" & vbLf)
            iPosInItem = 0
            BI.AUTHOR = sBText.Substring(7, sBText.IndexOf("TITLE") - 7).Trim
            BI.TITLE = GetTagVal(sBText, "TITLE", "BASENAME")
            BI.BASENAME = GetTagVal(sBText, "BASENAME", "STATUS")
            BI.STATUS = GetTagVal(sBText, "STATUS", "ALLOW COMMENTS")
            BI.ALLOWCOMMENTS = GetTagVal(sBText, "ALLOW COMMENTS", "CONVERT BREAKS")
            BI.CONVERTBREAKS = GetTagVal(sBText, "CONVERT BREAKS", "ALLOW PINGS")

            If sBText.IndexOf("PRIMARY CATEGORY") > -1 Then
                BI.ALLOWPINGS = GetTagVal(sBText, "ALLOW PINGS", "PRIMARY CATEGORY")
                BI.PRIMARYCATEGORY = GetTagVal(sBText, "PRIMARY CATEGORY", "CATEGORY")
                BI.CATEGORY = GetTagVal(sBText, "CATEGORY", "DATE")
            Else
                BI.ALLOWPINGS = GetTagVal(sBText, "ALLOW PINGS", "DATE")
            End If

            If sBText.IndexOf("TAGS:") > -1 Then
                BI.BlogDATE = fixDodgyDate(GetTagVal(sBText, "DATE", "TAGS"))
                BI.TAGS = GetTagVal(sBText, "TAGS", "BODY").Replace("-----", "")
            Else
                BI.BlogDATE = fixDodgyDate(GetTagVal(sBText, "DATE", vbLf))
            End If

            BI.BODY = GetTagVal(sBText, "BODY", "EXTENDED BODY").Replace("-----", "")
            BI.EXTENDEDBODY = GetTagVal(sBText, "EXTENDED BODY", "EXCERPT").Replace("-----", "")
            BI.EXCERPT = GetTagVal(sBText, "EXCERPT", "KEYWORDS").Replace("-----", "")
            If sBText.IndexOf("COMMENT:") > -1 Then
                BI.KEYWORDS = GetTagVal(sBText, "KEYWORDS", "COMMENT").Replace("-----", "")

                Dim sComments As String = sBText.Substring(sBText.IndexOf("COMMENT:", iPosInItem), sBText.Length - sBText.IndexOf("COMMENT:", iPosInItem))
                Dim iC1, iC2 As Integer
                iC1 = sComments.IndexOf("COMMENT:")
                Do While iC1 > -1
                    iPosInItem = 0
                    iC2 = sComments.IndexOf("-----" & vbLf)
                    Dim sComment As String
                    If iC2 > -1 Then
                        sComment = sComments.Substring(0, iC2).Trim
                    Else
                        sComment = sComments
                    End If

                    Dim BLCom As New BlogComments

                    BLCom.AUTHOR = GetTagVal(sComment, "AUTHOR", "EMAIL")
                    BLCom.EMAIL = GetTagVal(sComment, "EMAIL", "IP")
                    BLCom.IP = GetTagVal(sComment, "IP", "URL")
                    BLCom.URL = GetTagVal(sComment, "URL", "DATE")

                    BLCom.CommentDATE = fixDodgyDate(GetCommentDate(sComment))
                    BLCom.COMMENT = sComment.Substring(iPosInItem, sComment.Length - iPosInItem)

                    sComments = sComments.Substring(iC2 + 5, sComments.Length - (iC2 + 5)).Trim
                    iC1 = sComments.IndexOf("COMMENT:")
                    BI.blogcomments.Add(BLCom)
                Loop
            Else
                BI.KEYWORDS = GetTagVal(sBText, "KEYWORDS", "-----").Replace("-----", "")
            End If

            BlogItems.Add(BI)
            i = i + 1
        Loop
        '  Dim iEntries As Integer = sBlogentries.GetUpperBound(0)


    End Sub
    Private Shared Function fixDodgyDate(ByVal sDate As String) As String
        Dim sRet As String
        Dim sVals() As String = sDate.Split("/")
        sRet = sVals(1) & "/" & sVals(0) & "/" & sVals(2)
        Return sRet
    End Function
End Class
Public Class BlogComments
    Dim mCOMMENT As String
    Dim mAUTHOR As String
    Dim mEMAIL As String
    Dim mIP As String
    Dim mURL As String
    Dim mDATE As Date

    Public Property COMMENT() As String
        Get
            Return mCOMMENT
        End Get
        Set(ByVal value As String)
            mCOMMENT = value
        End Set
    End Property
    Public Property AUTHOR() As String
        Get
            Return mAUTHOR
        End Get
        Set(ByVal value As String)
            mAUTHOR = value
        End Set
    End Property
    Public Property EMAIL() As String
        Get
            Return mEMAIL
        End Get
        Set(ByVal value As String)
            mEMAIL = value
        End Set
    End Property
    Public Property IP() As String
        Get
            Return mIP
        End Get
        Set(ByVal value As String)
            mIP = value
        End Set
    End Property
    Public Property URL() As String
        Get
            Return mURL
        End Get
        Set(ByVal value As String)
            mURL = value
        End Set
    End Property
    Public Property CommentDATE() As Date
        Get
            Return mDATE
        End Get
        Set(ByVal value As Date)
            mDATE = value
        End Set
    End Property
End Class
Public Class BlogItem
    Dim mAUTHOR As String
    Dim mTITLE As String
    Dim mBASENAME As String
    Dim mSTATUS As String
    Dim mALLOWCOMMENTS As String
    Dim mCONVERTBREAKS As String
    Dim mALLOWPINGS As String
    Dim mPRIMARYCATEGORY As String
    Dim mCATEGORY As String
    Dim mDATE As Date
    Dim mTAGS As String

    Dim mBODY As String

    Dim mEXTENDEDBODY As String

    Dim mEXCERPT As String


    Dim mKEYWORDS As String

    Dim mcolComments As New Generic.List(Of BlogComments)

    Public Property EXTENDEDBODY() As String
        Get
            Return mEXTENDEDBODY
        End Get
        Set(ByVal value As String)
            mEXTENDEDBODY = value
        End Set
    End Property

    Public Property EXCERPT() As String
        Get
            Return mEXCERPT
        End Get
        Set(ByVal value As String)
            mEXCERPT = value
        End Set
    End Property


    Public Property KEYWORDS() As String
        Get
            Return mKEYWORDS
        End Get
        Set(ByVal value As String)
            mKEYWORDS = value
        End Set
    End Property
    Public Property blogcomments() As Generic.List(Of BlogComments)
        Get
            Return mcolComments
        End Get
        Set(ByVal value As Generic.List(Of BlogComments))
            mcolComments = value
        End Set
    End Property
    Public Property AUTHOR() As String
        Get
            Return mAUTHOR
        End Get
        Set(ByVal value As String)
            mAUTHOR = value
        End Set
    End Property
    Public Property TITLE() As String
        Get
            Return mTITLE
        End Get
        Set(ByVal value As String)
            mTITLE = value
        End Set
    End Property
    Public Property BASENAME() As String
        Get
            Return mBASENAME
        End Get
        Set(ByVal value As String)
            mBASENAME = value
        End Set
    End Property
    Public Property STATUS() As String
        Get
            Return mSTATUS
        End Get
        Set(ByVal value As String)
            mSTATUS = value
        End Set
    End Property
    Public Property ALLOWCOMMENTS() As String
        Get
            Return mALLOWCOMMENTS
        End Get
        Set(ByVal value As String)
            mALLOWCOMMENTS = value
        End Set
    End Property
    Public Property CONVERTBREAKS() As String
        Get
            Return mCONVERTBREAKS
        End Get
        Set(ByVal value As String)
            mCONVERTBREAKS = value
        End Set
    End Property
    Public Property ALLOWPINGS() As String
        Get
            Return mALLOWPINGS
        End Get
        Set(ByVal value As String)
            mALLOWPINGS = value
        End Set
    End Property
    Public Property PRIMARYCATEGORY() As String
        Get
            Return mPRIMARYCATEGORY
        End Get
        Set(ByVal value As String)
            mPRIMARYCATEGORY = value
        End Set
    End Property
    Public Property CATEGORY() As String
        Get
            Return mCATEGORY
        End Get
        Set(ByVal value As String)
            mCATEGORY = value
        End Set
    End Property
    Public Property BlogDATE() As Date
        Get
            Return mDATE
        End Get
        Set(ByVal value As Date)
            mDATE = value
        End Set
    End Property

    Public Property TAGS() As String
        Get
            Return mTAGS
        End Get
        Set(ByVal value As String)
            mTAGS = value
        End Set
    End Property

    Public Property BODY() As String
        Get
            Return mBODY
        End Get
        Set(ByVal value As String)
            mBODY = value
        End Set
    End Property

    Public Sub New()

    End Sub
End Class
